package ch.usi.inf.pf2.editor;

import ch.usi.inf.pf2.Sheet;
import ch.usi.inf.pf2.cell.Cell;

/**
 * Used to change the cell length
 * 
 * @author Lorenzo Baracchi
 *
 */
public class ChangeCellLength extends CellManagement
{
    private int oldLength;
    private int newLength;
    
    
    /**
     * Create an object able to change the length of the cell.
     * @param sheet The sheet where the cell is.
     * @param rowIndex The numeric index of the cell.
     * @param columnIndex The literal index of the cell.
     * @param newLength The new length wanted for the cell.
     */
    public ChangeCellLength (Sheet sheet, int rowIndex, int columnIndex, int newLength)
    {
	   super(sheet, rowIndex, columnIndex);
	   this.setNewLength(newLength);
    }

    
    /**
     * Change the length of the cell and store the old one
     * into oldLength for possible undo actions.
     */
    @Override
    public void evaluate ()
    {
	   Cell c = sheet.getCell(getColumnIndex(), getRowIndex());
	   setOldLength(c.getCellLength());
	   c.setCellLength(newLength);
    }

    
    /**
     * Allow to set in the field oldLength
     * a wanted old length for the cell.
     * @param oldLength The old length of the cell.
     */
    public void setOldLength (int oldLength)
    {
	   this.oldLength = oldLength;
    }

    
    /**
     * @return The old length of the cell.
     */
    public int getOldLength ()
    {
	   return oldLength;
    }

    
    /**
     * Allow to set in the field newLength
     * the new length of the cell.
     * @param newLength The new length of the cell.
     */
    public void setNewLength (int newLength)
    {
	   this.newLength = newLength;
    }

    
    /**
     * @return The new length of the cell.
     */
    public int getNewLength ()
    {
	   return newLength;
    }

    
    /**
     * Undo the previous change of length.
     */
    @Override
    public void restore ()
    {
	   Cell c = sheet.getCell(getColumnIndex(), getRowIndex());
	   c.setCellLength(getOldLength());
    }



}
